﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Tesla.Service.DataContracts;
using Tesla.DataAccess.DataAccessComponents;
using Tesla.Util.Utilities;
using Tesla.DataAccess.DataAccessLogic.Interfaces;

namespace Tesla.DataAccess.DataAccessLogic.Implementations
{
    public class EmployeeDA : BaseOperationalDA
    {
        #region BaseOperationalDA Members
        public override int? Create(BaseDC bDC)
        {
            int success = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;

                    success = context.usp_CreateEmployee(
                                                SetSafeString(((EmployeeDC)bDC).EmployeeFirstName),
                                                SetSafeString(((EmployeeDC)bDC).EmployeeLastName),
                                                SetSafeString(((EmployeeDC)bDC).EmployeeMiddleName),
                                                SetSafeString(((EmployeeDC)bDC).EmployeePANNo),
                                                SetSafeInt(((EmployeeDC)bDC).EmployeeContactNo),
                                                SetSafeString(((EmployeeDC)bDC).EmployeeEmailID),
                                                SetSafeInt(((EmployeeDC)bDC).EmployeeRelevantExp),
                                                ((EmployeeDC)bDC).EmployeePicture);


                    context.SubmitChanges();
                    return success;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override int Update(BaseDC bDC)
        {
            int recordCount = 0;

            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    recordCount = context.usp_UpdateEmployee(
                                                SetSafeInt(((EmployeeDC)bDC).EmployeeID),
                                                SetSafeString(((EmployeeDC)bDC).EmployeeFirstName),
                                                SetSafeString(((EmployeeDC)bDC).EmployeeLastName),
                                                SetSafeString(((EmployeeDC)bDC).EmployeeMiddleName),
                                                SetSafeString(((EmployeeDC)bDC).EmployeePANNo),
                                                SetSafeInt(((EmployeeDC)bDC).EmployeeContactNo),
                                                SetSafeString(((EmployeeDC)bDC).EmployeeEmailID),
                                                SetSafeInt(((EmployeeDC)bDC).EmployeeRelevantExp),
                                                ((EmployeeDC)bDC).EmployeePicture);
                    context.SubmitChanges();
                    return recordCount;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> DisplayAll(int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> employees = null;
            try
            {
                
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_DisplayAllEmployeeResult> employeeList = context.usp_DisplayAllEmployee(gridSize, ref totalRecordCount).Cast<usp_DisplayAllEmployeeResult>();
                    employees = GenericConversion.ConvertToTargetType(employeeList.ToList<usp_DisplayAllEmployeeResult>().ToArray(), typeof(usp_DisplayAllEmployeeResult), typeof(EmployeeDC));
                    
                }

                return employees;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> SearchByName(string name, int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> employees = null;

            try
            {
                
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_SearchEmployeeByNameResult> employeeList = context.usp_SearchEmployeeByName(name, gridSize, ref totalRecordCount).Cast<usp_SearchEmployeeByNameResult>();
                    employees = GenericConversion.ConvertToTargetType(employeeList.ToList<usp_SearchEmployeeByNameResult>().ToArray(), typeof(usp_SearchEmployeeByNameResult), typeof(EmployeeDC));                    
                }
                return employees;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> Read(int id)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadEmployeeResult> employeeList = context.usp_ReadEmployee(id).Cast<usp_ReadEmployeeResult>();
                    return GenericConversion.ConvertToTargetType(employeeList.ToList<usp_ReadEmployeeResult>().ToArray(), typeof(usp_ReadEmployeeResult), typeof(EmployeeDC));
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override IList<BaseDC> ReadByRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            try
            {
                using (TeslaDataContext context = new TeslaDataContext())
                {
                    context.DeferredLoadingEnabled = false;
                    IEnumerable<usp_ReadEmployeeByRowNumberRangeResult> employeeList = context.usp_ReadEmployeeByRowNumberRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString).Cast<usp_ReadEmployeeByRowNumberRangeResult>();
                    return GenericConversion.ConvertToTargetType(employeeList.ToList<usp_ReadEmployeeByRowNumberRangeResult>().ToArray(), typeof(usp_ReadEmployeeByRowNumberRangeResult), typeof(EmployeeDC));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
